package com.hdx.server.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.hdx.common.constant.JwtClaimsConstant;
import com.hdx.common.constant.MessageConstant;
import com.hdx.common.constant.PasswordConstant;
import com.hdx.common.constant.StatusConstant;


import com.hdx.common.exception.AccountLockedException;
import com.hdx.common.exception.AccountNotFoundException;
import com.hdx.common.exception.PasswordErrorException;


import com.hdx.common.properties.JwtProperties;
import com.hdx.common.result.PageResult;
import com.hdx.common.utils.JwtUtil;
import com.hdx.model.dto.EmployeeDTO;
import com.hdx.model.dto.EmployeeLoginDTO;
import com.hdx.model.dto.EmployeePageQueryDTO;
import com.hdx.model.entity.Employee;
import com.hdx.model.vo.EmployeeLoginVO;
import com.hdx.server.mapper.EmployeeMapper;
import com.hdx.server.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


import java.util.HashMap;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private JwtProperties jwtProperties;
    //员工登录
    @Override
    public EmployeeLoginVO login(EmployeeLoginDTO employeeLoginDTO) throws AccountLockedException {
        //抛出异常:账号不存在,账号密码错误,账号状态封禁
        String username = employeeLoginDTO.getUsername();
        Employee employee=employeeMapper.selectByUserName(username);
        if (employee==null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        String password = employeeLoginDTO.getPassword();
        String inputPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!inputPassword.equals(employee.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        Integer status = employee.getStatus();
        if(status== StatusConstant.DISABLE){
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }



        //生成JWT令牌
        HashMap<String , Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        //返回employeeLoginVO
        EmployeeLoginVO employeeLoginVO = new EmployeeLoginVO();
        employeeLoginVO.setId(employee.getId());
        employeeLoginVO.setName(employee.getName());
        employeeLoginVO.setUserName(employee.getUsername());
        employeeLoginVO.setToken(token);
        return employeeLoginVO;

    }

    //员工分页查询
    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {

        //Pagehelper
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        //Page
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        //PageResult(total,records)
        return new PageResult(page.getTotal(),page.getResult());
    }

    //新增员工
    @Override
    public void save(EmployeeDTO employeeDTO) {
        //设置初始密码123456
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setPassword(PasswordConstant.DEFAULT_PASSWORD);
        //设置账号状态为使用
        employee.setStatus(StatusConstant.ENABLE);

        employeeMapper.insert(employee);

    }
    //启用禁用员工
    @Override
    public void startOrStop(Integer status, Long id) {
        //build()
        Employee employee = Employee.builder()
                .id(id)
                .status(status)
                .build();
        employeeMapper.update(employee);
    }

    //根据id查询员工信息
    @Override
    public Employee getById(Long id) {
        //设置密码(******)
        Employee employee=employeeMapper.getById(id);
        employee.setPassword("******");
        return employee;
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employeeMapper.update(employee);
    }
}
